﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using MT.eVotePRS.Model;
using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Presenter;

namespace MT.eVotePRS.UI.UserControls
{
    public partial class CheckHandset : UserControl, IViewCheckStatus
    {
        #region IViewCheckStatus

        CheckingStatus _toCheck;
        Handsets _handset;
        StudentInfo _student;
        InventoryTxn _txn;

        public CheckingStatus ToCheck { get { return _toCheck; } }

        public Handsets Handset
        {
            get
            {
                return _handset;
            }
            set
            {
                _handset = value;
            }
        }

        public StudentInfo Student
        {
            get
            {
                return _student;
            }
            set
            {
                _student = value;
                lblShowStudentBarcode.Text = _student.Barcode;
                lblShowStudentNo.Text = _student.StudentNo;
                lblShowStudentName.Text = _student.StudentName;
                lblShowGraduate.Text = _student.IsGraduate ? "Yes" : "No";
            }
        }

        public InventoryTxn Txn
        {
            set
            {
                _txn = value;
                lblShowStatus.Text = _txn.UserEvent;
                lblShowDateTime.Text = _txn.TxnTime.ToShortDateString();
            }
        }

        public event EventHandler IdEntered;

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public CheckHandset()
        {
            InitializeComponent();
            txtHandsetID.KeyDown += OnKeyDown;
            txtHandsetID.KeyUp += OnKeyUp;

            _toCheck = CheckingStatus.Handset;
        }

        void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(txtHandsetID.Text))
                {
                    RaiseIdEntered();
                }
            }
        }

        void OnKeyUp(object sender, KeyEventArgs e)
        {
            if (string.IsNullOrEmpty(txtHandsetID.Text))
            {
                _handset.MAC = txtHandsetID.Text;
                RaisePropertyChanged("HANDSET");
            }
        }

        protected virtual void RaiseIdEntered()
        {
            EventHandler handlers = IdEntered;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handlers = PropertyChanged;
            if (handlers != null)
            {
                handlers(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
